package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.rdb.api.EcMaterielCategoryRdbService;
import com.ndp.fb.rdb.model.EcMaterielCategory;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.EcMaterielCategoryService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hexin on 2016/5/24.
 */
@Component
public class EcMaterielCategoryServiceImpl implements EcMaterielCategoryService {

    @Reference
    EcMaterielCategoryRdbService ecMaterielCategoryRdbService;
    @Override
    public List<EcMaterielCategory> findSupportCreate(User user) {
        String language =  user.getLanguage();
        List<EcMaterielCategory> list;
        if ("zh-cn".equalsIgnoreCase(language) || "zh_CN".equalsIgnoreCase(language)
                || "zh".equalsIgnoreCase(language) || "CN".equalsIgnoreCase(language)) {
            list = ecMaterielCategoryRdbService.findSupportedCN();
        } else  {
            list = ecMaterielCategoryRdbService.findSupportedUS();
        }
        return buildBranchTree(list);
    }

    @Override
    public List<EcMaterielCategory> findAll(User user) {
        Long orgId = user.getOrgId();
        List<EcMaterielCategory> list = null;
        String language =  user.getLanguage();
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            list = ecMaterielCategoryRdbService.findAllCN();
        } else  {
            list = ecMaterielCategoryRdbService.findAllUS();
        }
        //增加运行状态
        List<EcMaterielCategory> tempList = buildBranchTree(list);
        return tempList;
    }

    @Override
    public List<EcMaterielCategory> findSupportTemplateFilter(User user) {
        String language =  user.getLanguage();
        List<EcMaterielCategory> list;
        if ("zh-cn".equalsIgnoreCase(language) || "zh_CN".equalsIgnoreCase(language)
                || "zh".equalsIgnoreCase(language) || "CN".equalsIgnoreCase(language)) {
            list = ecMaterielCategoryRdbService.findSupportTemplateFilterCN();
        } else  {
            list = ecMaterielCategoryRdbService.findSupportTemplateFilterUS();
        }
        return buildBranchTree(list);
    }

    @Override
    public List<EcMaterielCategory> supportMaterielFilter(User user) {
        String language =  user.getLanguage();
        List<EcMaterielCategory> list;
        if ("zh-cn".equalsIgnoreCase(language) || "zh_CN".equalsIgnoreCase(language)
                || "zh".equalsIgnoreCase(language) || "CN".equalsIgnoreCase(language)) {
            list = ecMaterielCategoryRdbService.findSupportMaterielFilterCN();
        } else  {
            list = ecMaterielCategoryRdbService.findSupportMaterielFilterUS();
        }
        return buildBranchTree(list);
    }

    private List<EcMaterielCategory> buildBranchTree(List<EcMaterielCategory> branchs) {
        List<EcMaterielCategory> top = new ArrayList<>();
        for (Iterator iterator = branchs.iterator(); iterator.hasNext(); ) {
            EcMaterielCategory branch = (EcMaterielCategory) iterator.next();
            if (0 == branch.getPid()) {
                top.add(branch);
                iterator.remove();
            }
        }

        for (EcMaterielCategory ecMaterielCategory : top) {
            buildTree(ecMaterielCategory, branchs);
        }
        return top;
    }

    private void buildTree(EcMaterielCategory parent, List<EcMaterielCategory> branchs) {
        for (Iterator iterator = branchs.iterator(); iterator.hasNext(); ) {
            EcMaterielCategory branch = (EcMaterielCategory) iterator.next();
            if (branch.getPid() == parent.getId()) {
                parent.getChildren().add(branch);
                iterator.remove();
            }
        }
        if (branchs.size() == 0)
            return;
        if (null != parent.getChildren()) {
            for (EcMaterielCategory branch2 : parent.getChildren()) {
                buildTree(branch2, branchs);
            }
        }
    }
}
